package com.com.xinke.edu.petmall.fragment.home;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.chad.library.adapter4.BaseQuickAdapter;
import com.com.xinke.edu.petmall.activity.ProductDetailActivity;
import com.com.xinke.edu.petmall.databinding.FragmentHomeBinding;
import com.com.xinke.edu.petmall.entity.Product;
import com.com.xinke.edu.petmall.entity.ProductCategory;
import com.com.xinke.edu.petmall.entity.ResultBody;
import com.com.xinke.edu.petmall.fragment.home.adpter.ProductAdapter;
import com.com.xinke.edu.petmall.fragment.home.adpter.ProductTypeAdapter;
import com.com.xinke.edu.petmall.util.GridSpacingItemDecoration;
import com.com.xinke.edu.petmall.util.RetrofitUtil;

import java.util.List;

import io.reactivex.rxjava3.android.schedulers.AndroidSchedulers;
import io.reactivex.rxjava3.core.Observer;
import io.reactivex.rxjava3.disposables.Disposable;
import io.reactivex.rxjava3.schedulers.Schedulers;

public class HomeFragment extends androidx.fragment.app.Fragment {
    FragmentHomeBinding binding;
    ProductTypeAdapter adapter;
    ProductAdapter productAdapter;

    int typeId = 0;
    int pageIndex = 1;
    int pageSize = 10;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        binding = FragmentHomeBinding.inflate(inflater);
        return binding.getRoot();
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        adapter = new ProductTypeAdapter();
        binding.typeRecyclerview.setAdapter(adapter);
        LinearLayoutManager manager = new LinearLayoutManager(getContext());
        manager.setOrientation(LinearLayoutManager.HORIZONTAL);
        binding.typeRecyclerview.setLayoutManager(manager);
        getType();
        adapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener<ProductCategory>() {
            @Override
            public void onClick(@NonNull BaseQuickAdapter<ProductCategory, ?> baseQuickAdapter, @NonNull View view, int i) {
                List<ProductCategory> datas = adapter.getItems();
                for (int j = 0; j < datas.size(); j++) {
                    ProductCategory type = datas.get(j);
                    type.setSelect(i == j);
                    if (i == j) {
                        typeId = type.getId();
                        pageIndex = 1;
                        getProduct();
                    }
                }
                adapter.notifyDataSetChanged();
            }
        });


        productAdapter = new ProductAdapter();
        binding.productRecyclerview.setAdapter(productAdapter);
        GridLayoutManager gridLayoutManager = new GridLayoutManager(getContext(), 2);
        binding.productRecyclerview.setLayoutManager(gridLayoutManager);
        binding.productRecyclerview.addItemDecoration(new GridSpacingItemDecoration(2, 10, true, 0));
        getProduct();
        productAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener<Product>() {
            @Override
            public void onClick(@NonNull BaseQuickAdapter<Product, ?> baseQuickAdapter, @NonNull View view, int i) {
                Product product = productAdapter.getItem(i);
                Intent intent = new Intent(getContext(), ProductDetailActivity.class);
                intent.putExtra("product", product);
                startActivity(intent);
            }
        });
    }


    public void getType() {
        RetrofitUtil.getApiService().getType()
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<ResultBody<List<ProductCategory>>>() {
                    @Override
                    public void onSubscribe(Disposable d) {

                    }

                    @Override
                    public void onNext(ResultBody<List<ProductCategory>> listResultBody) {
                        List<ProductCategory> datas = listResultBody.getData();
                        ProductCategory productCategory = new ProductCategory();
                        productCategory.setId(0);
                        productCategory.setName("全部");
                        productCategory.setSelect(true);
                        datas.add(0, productCategory);
                        adapter.addAll(datas);
                    }

                    @Override
                    public void onError(Throwable e) {
                        Toast.makeText(getContext(), e.getMessage(), Toast.LENGTH_SHORT).show();
                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }

    public void getProduct() {
        String keyword = binding.keyword.getText().toString();
        RetrofitUtil.getApiService().getProduct(keyword, typeId, pageIndex, pageSize)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<ResultBody<List<Product>>>() {
                    @Override
                    public void onSubscribe(@io.reactivex.rxjava3.annotations.NonNull Disposable d) {

                    }

                    @Override
                    public void onNext(@io.reactivex.rxjava3.annotations.NonNull ResultBody<List<Product>> listResultBody) {
                        List<Product> datas = listResultBody.getData();
                        if (pageIndex == 1) {
                            productAdapter.submitList(datas);
                        } else {
                            productAdapter.addAll(datas);
                        }
                    }

                    @Override
                    public void onError(@io.reactivex.rxjava3.annotations.NonNull Throwable e) {
                        Toast.makeText(getContext(), e.getMessage(), Toast.LENGTH_SHORT).show();
                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }
}
